package com.wondertek.poms.dao.repo;

import java.time.LocalDateTime;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.wondertek.poms.dao.po.PrdInfo;

/**
 * @author chenjijiang
 * @description
 */
@Repository
public interface IPrdInfoDao extends JpaRepository<PrdInfo, Long>, JpaSpecificationExecutor<PrdInfo> {

	@Query(value = "select * from PRD_INFO pi where pi.PRODUCT_INFO_PACKAGE_ID=?1 and (pi.SYSTEM_FLAG = 2 " +
			"or pi.SYSTEM_FLAG is NULL) order by pi.ENTER_PRDINFO_PRIORITY", nativeQuery = true)
	List<PrdInfo> findAllByProductInfoPackageId(Long id);

	/**
	 * <p><b>Title:</b> getNotPackageProductInfo</p>
	 * <p><b>Description:</b> 获取没有二级产品的商品包，且包是发布状态</p>
	 *
	 * @return
	 * @author douzi
	 */
	@Query(value = "select * from PRD_INFO pi where pi.PRODUCT_INFO_PACKAGE_ID is null and pi.IS_PUBLISH = 2", nativeQuery = true)
	List<PrdInfo> getNotPackagePrdInfo();

	/**
	 * <p><b>Title:</b> findPrdInfoInPackageId</p>
	 * <p><b>Description:</b> 根据二级产品id查询poms的商品包列表，且包是发布状态</p>
	 *
	 * @param productInfoPackageId 二级产品id
	 * @param publishStatus
	 * @return
	 * @author douzi
	 */
	@Query(value = "select * from PRD_INFO pi where pi.PRODUCT_INFO_PACKAGE_ID=?1 and pi.IS_PUBLISH = ?2 and (pi.system_flag=2 or pi.system_flag is null)  order by pi.enter_prdinfo_priority desc", nativeQuery = true)
	List<PrdInfo> findPrdInfoInPackageId(Long productInfoPackageId, int publishStatus);

	/**
	 * <p><b>Title:</b> findPrdInfoInPackageId</p>
	 * <p><b>Description:</b> 获取二级产品是poms的商品包列表，且包是发布状态 </p>
	 * @author 高正行
	 * @return 获取二级产品是poms的商品包列表
	 */
	@Query(value = "select * from PRD_INFO pi where pi.IS_PUBLISH = 2 and (pi.system_flag=2 or pi.system_flag is null)  order by pi.enter_prdinfo_priority desc", nativeQuery = true)
	List<PrdInfo> findPrdInfoByIsPublish(int publishStatus);

	/**
	 * <p><b>Title:</b> findPrdInfoInPackageId</p>
	 * <p><b>Description:</b> 获取二级产品是poms的有父商品包的列表，且包是发布状态 </p>
	 * @param publishStatus 是否发布  1：未发布 2：已发布 3：已撤回
	 * @param prdInfoIdS 商品包id,分隔
	 * @author 高正行
	 * @return 获取二级产品是poms的商品包列表
	 */
	@Query(value = "select * from PRD_INFO pi where pi.IS_PUBLISH = ?1 and pi.PRDINFOID in (?2) " +
			" and pi.PARENT_PRDINFO_ID is not null and (pi.system_flag=2 or pi.system_flag is null)", nativeQuery = true)
	List<PrdInfo> findParentPrdInfo(int publishStatus,List<Long>  prdInfoIdS);

	/**
	 * <p><b>Title:</b> findPrdInfoInPackageId</p>
	 * <p><b>Description:</b> 获取二级产品是poms的商品包的列表，且包是发布状态 </p>
	 * @param publishStatus 是否发布  1：未发布 2：已发布 3：已撤回
	 * @param prdInfoId 商品包id
	 * @author 高正行
	 * @return 获取二级产品是poms的商品包列表
	 */
	@Query(value = "select pi.PRODUCT_INFO_PACKAGE_ID from PRD_INFO pi where pi.IS_PUBLISH = ?1 and pi.PRDINFOID = ?2 " +
			"  and (pi.system_flag=2 or pi.system_flag is null)", nativeQuery = true)
	Long find (int publishStatus,Long  prdInfoId);

	/**
	 * <p><b>Title:</b> findPrdInfoInPackageId</p>
	 * <p><b>Description:</b> 获取二级产品是poms的有父商品包的列表，且包是发布状态 </p>
	 * @param publishStatus 是否发布  1：未发布 2：已发布 3：已撤回
	 * @author 高正行
	 * @return 获取二级产品是poms的商品包列表
	 */
	@Query(value = "select * from PRD_INFO pi where pi.IS_PUBLISH = ?1 " +
			" and pi.PARENT_PRDINFO_ID is not null and (pi.system_flag=2 or pi.system_flag is null)", nativeQuery = true)
	List<PrdInfo> findAllParentPrdInfo(int publishStatus);

	PrdInfo findByPrdInfoId(Long prdInfoId);

	@Query(value = "update PRD_INFO set IS_PUBLISH = ?1 where PRDINFOID in (?2)", nativeQuery = true)
	@Modifying
	@Transactional
	int updateIsPublish(Integer status, List<Long> prdInfoIds);

	/**
	 * <p><b>Title:</b> getTotalUseOfEquityid</p>
	 * <p><b>Description:</b> 查看商品包是否使用权益节点</p>
	 */
	@Query(value = "select count(*) from PRD_INFO where EQUITY_ID like ?1 ",
			nativeQuery = true)
	int getTotalUseOfEquityid(String equityId);

	@Query(value = "update PRD_INFO set IS_PUBLISH = ?1,PUB_TIME=?2  where PRDINFOID in (?3)", nativeQuery = true)
	@Modifying
	@Transactional
	int updatePrdInfoPublish(Integer status, LocalDateTime now, List<Long> list);

	@Query(value = "select * from PRD_INFO where IS_PUBLISH=2 and PRDINFOID!=?1", nativeQuery = true)
	List<PrdInfo> findAllTargetPrdInfo(Long prdInfoId);

	@Query(value = "select * from PRD_INFO where IS_PUBLISH=2", nativeQuery = true)
	List<PrdInfo> findAllPublishedPrdInfo();

	@Query(value="select * from PRD_INFO pi where pi.PRDINFOID=?1 and (pi.system_flag=2 or pi.system_flag is null)", nativeQuery=true)
	PrdInfo getByPrdInfoIdAndSystemFlag(Long prdInfoId);

	/**
	 * <p><b>Title:</b> findProductInfoPackageId</p>
	 * <p><b>Description:</b> 根据商品包获取二级产品id，判断是否是二级产品下的商品包</p>
	 * @author douzi
	 * @param prdInfoId
	 * @return
	 */
	@Query(value="select PRODUCT_INFO_PACKAGE_ID from PRD_INFO pi where pi.PRDINFOID=?1", nativeQuery=true)
	Long findProductInfoPackageId(Long prdInfoId);
}
